<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="java.sql.*"%>
<%@page import="javax.swing.JOptionPane"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
/** 链接数据库参数 **/
    String driverName = "com.mysql.jdbc.Driver"; //驱动名称
    String DBUser = "root"; //mysql用户名
    String DBPasswd = "123456"; //mysql密码
    String DBName = "user"; //数据库名
	String username=request.getParameter("username");
    String password=request.getParameter("passwd");
    //数据库完整链接地址
    String connUrl = "jdbc:mysql://localhost:3306/" + DBName + "?user="
            + DBUser + "&password=" + DBPasswd;

    //加载数据库驱动
    Class.forName(driverName).newInstance();

    //链接数据库并保存到 conn 变量中
    Connection conn = DriverManager.getConnection(connUrl);

    //申明
    Statement stmt = conn.createStatement();

    //设置字符集
    stmt.executeQuery("SET NAMES UTF8");

    //要执行的 sql 查询
	String sql = "select * from user where username='"+username+"' and password='"+password+"'";
    String sql1 = "insert into user(username,password) values('"+username+"','"+password+"')";
    String sql2 = "update user set password = '"+password+"' where username = '"+ username+"'";
    String sql3 = "delete from user where username = '"+username+"'";
    String sql4 = "select * from user where username = '"+username+"'";
    	
    //取得结果
    //ResultSet rs = stmt.executeQuery(sql);
    //ResultSet rs1 = stmt.executeQuery(sql1);
   	//ResultSet rs2 = stmt.executeQuery(sql2);
    ResultSet rs = stmt.executeQuery(sql4);
    String   action   =   request.getParameter("action");  
  	if   (action.equals("zhuce"))  
  	{  
		//ResultSet rs = stmt.executeQuery(sql4);
		if(rs.next())
		JOptionPane.showMessageDialog(null,"用户名已存在");
		else
		{
			rs=stmt.executeQuery(sql1);
			JOptionPane.showMessageDialog(null,"注册成功");
		}
  	}  
  	if   (action.equals("denglu"))  
  	{  
  		rs = stmt.executeQuery(sql);
  		if(rs.next())
  		JOptionPane.showMessageDialog(null,"登陆成功");
  		else
  		JOptionPane.showMessageDialog(null,"请检查用户名或密码");
  	}  
  	if   (action.equals("tingyong"))  
  	{  
		rs = stmt.executeQuery(sql4);
		if(rs.next())
		{
		rs = stmt.executeQuery(sql);
		if(rs.next())
			{
			rs = stmt.executeQuery(sql3);
			JOptionPane.showMessageDialog(null,"用户名已停用");
			}
		else
			JOptionPane.showMessageDialog(null,"密码错误");
		}
		else
		JOptionPane.showMessageDialog(null,"用户名不存在");
  	}  
  	if   (action.equals("xiugaimima"))
  	{  
		 rs = stmt.executeQuery(sql4);
		 if(rs.next())
		 {
		 rs = stmt.executeQuery(sql2);//有点小bug没调
		 JOptionPane.showMessageDialog(null,"修改成功");
		 }
		 else 
		 JOptionPane.showMessageDialog(null,"用户名不存在");
  	}
  	    /** 关闭连接 **/
    conn.close();
    stmt.close();
    rs.close(); 
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    <form id="form1" name="form1" method="post" >
  <p>用户名
    <label for="textfield"></label>
    <input type="text" name="username" id="textfield" />
  </p>
  <p>
    <label for="textfield2"></label>
    密      码
     <input type="text" name="password" id="textfield2" />
  </p>
  <p>
    <input type="submit" name="button"  value="注册" onclick="document.Form1.action.value='zhuce'"/>
    <input type="submit" name="button"  value="登陆" onclick="document.Form1.action.value='denglu'"/>
    <input type="submit" name="button"  value="停用" onclick="document.Form1.action.value='tingyong'"/>
    <input type="submit" name="button"  value="修改密码" onclick="document.Form1.action.value='xiugaimima'"/>
  </p>
</form>
  </body>
</html>

